package com.future.message.Controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.future.message.Mapper.UserMapper;
import com.future.message.Service.FileService;
import com.future.message.Utils.ResponseResult;
import com.future.message.Utils.uploadUtil;
import com.future.message.pojo.SecurityUser;
import com.future.message.pojo.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/company/public")
public class FileController {
    @Resource
    FileService fileService;
    @Resource
    UserMapper userMapper;



    //项目根路径下的目录:springboot默认static
    public final static String UPLOAD_PATH_PREFIX="/static/upload/";
    //访问目录:
    public final String DOWNLOAD_PATH_PREFIX="/upload";



    @ResponseBody
    @PostMapping("/upload")
    public Object uploadFile(MultipartFile file, Integer userId, Integer relevantReportId){
        return fileService.uploadRelevantFile(file, userId, relevantReportId);
    }


    @PostMapping("/batchUpload")
    public ResponseResult batchUpload(MultipartFile[] files,Integer userId,Integer relevantReportId){
        return fileService.batchUploadFile(files,userId,relevantReportId);
    }

    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "name", value = "文件名字",required = true),
                    @ApiImplicitParam(name = "dataId", value = "这条数据的id",required = true),
                    @ApiImplicitParam(name = "userId", value = "当前登录的用户id",required = true)
            }
    )
    @DeleteMapping("/deleteTest")
    public ResponseResult deleteFile(String name,Integer dataId,Integer userId){
        return fileService.deleteFile(name,dataId, userId);
    }

    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "fileName", value = "文件名字",required = true),
                    @ApiImplicitParam(name = "relevantReportId", value = "填报关联id",required = true),
                    @ApiImplicitParam(name = "userId", value = "当前登录的用户id",required = true)
            }
    )
    @GetMapping("/downLoad")
    public void downLoad( Integer userId, Integer relevantReportId, String fileName, HttpServletResponse response){
        fileService.downLoadRelevantFile(userId, relevantReportId, fileName,response);
    }


    @PostMapping("/extendsFile")
    public void extendsFile(Integer oldIndexId,Integer newIndexId){
        fileService.extendsFile(oldIndexId, newIndexId);
    }


}


